/**
 * 将十六进制颜色转换为ASS颜色格式(BBGGRR)
 * @param {string} hexColor - 十六进制颜色 (#RRGGBB)
 * @returns {string} ASS颜色格式 (&HBBGGRR&)
 */
function convertToASSColor(hexColor) {
  if (!hexColor) return "&HFFFFFF&";

  // 移除#前缀
  const cleanHex = hexColor.replace("#", "");

  // 确保是6位十六进制颜色
  if (cleanHex.length !== 6) return "&HFFFFFF&";

  // 转换RRGGBB为BBGGRR
  const rr = cleanHex.substring(0, 2);
  const gg = cleanHex.substring(2, 4);
  const bb = cleanHex.substring(4, 6);

  return `&H${bb}${gg}${rr}&`;
}

/**
 * 生成ASS字幕文件的样式部分
 * @param {object} textStyles - 文本样式配置
 * @returns {string} ASS样式定义
 */
function generateASSStyles(textStyles) {
  // 增大默认字体大小
  const fontSize = textStyles.fontSize || 20; // 从14增大到20
  const fontFamily = textStyles.fontFamily || "Arial";
  const color = convertToASSColor(textStyles.color) || "&HFFFFFF&";
  const backgroundColor = convertToASSColor(textStyles.backgroundColor) || "&H000000&";
  const highlightBackgroundColor = convertToASSColor(textStyles.highlightBackgroundColor) || "&HFFFF00&";

  return `[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,${fontFamily},${fontSize},${color},&H000000FF,${backgroundColor},&H80000000,0,0,0,0,100,100,0,0,1,2,1,2,10,10,10,1
Style: Previous,${fontFamily},${fontSize - 6},${color},&H000000FF,${backgroundColor},&H80000000,0,0,0,0,100,100,0,0,1,1,1,2,10,10,30,1
Style: Current,${fontFamily},${fontSize},${color},&H000000FF,${highlightBackgroundColor},&H80000000,1,0,0,0,100,100,0,0,1,2,1,2,10,10,${textStyles.MarginV||150},1
Style: Next,${fontFamily},${fontSize - 6},${color},&H000000FF,${backgroundColor},&H80000000,0,0,0,0,100,100,0,0,1,1,1,2,10,10,110,1
`;
}

/**
 * 生成ASS字幕文件的样式部分
 * @param {object} textStyles - 文本样式配置
 * @returns {string} ASS样式定义
 */
function generateASSStylesSingleLine(textStyles) {
  // 增大默认字体大小
  const fontSize = textStyles.fontSize || 20; // 从14增大到20
  const fontSize_cn = 48;
  const fontFamily = textStyles.fontFamily || "Arial";
  const color = convertToASSColor(textStyles.color) || "&HFFFFFF&";
  const color_cn = "&H00FFFF&";
  const highlightBackgroundColor = convertToASSColor(textStyles.highlightBackgroundColor) || "&HFFFF00&";

  return `[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Current,${fontFamily},${fontSize},${color},&H000000FF,${highlightBackgroundColor},&H80000000,1,0,0,0,100,100,0,0,1,2,1,2,50,50,380,1
Style: Current_cn,${fontFamily},${fontSize_cn},${color_cn},&H000000FF,${highlightBackgroundColor},&H80000000,1,0,0,0,100,100,0,0,1,2,1,2,50,50,330,1
`;
}

/**
 * 将SRT时间格式转换为ASS时间格式
 * @param {string} srtTime - SRT时间格式 (HH:MM:SS,mmm)
 * @returns {string} ASS时间格式 (H:MM:SS.cc)
 */
function convertSRTToASSTime(srtTime) {
  // 将 HH:MM:SS,mmm 转换为 H:MM:SS.cc 格式
  const parts = srtTime.split(/[:,]/);
  if (parts.length !== 4) return srtTime;

  const hours = parseInt(parts[0], 10);
  const minutes = parseInt(parts[1], 10);
  const seconds = parseInt(parts[2], 10);
  const milliseconds = parseInt(parts[3], 10);

  // 转换毫秒为厘秒(100分之一秒)，并确保两位数格式
  const centiseconds = Math.floor((milliseconds % 1000) / 10);

  // 确保时间格式正确，处理小时数为单数字格式
  return `${hours % 24}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}.${centiseconds.toString().padStart(2, "0")}`;
}

module.exports = {
  convertToASSColor,
  generateASSStyles,
  convertSRTToASSTime,
  generateASSStylesSingleLine
};
